package org.study.swing.study01.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月14日
 */
public class ButtonDemo {
    public static void main(String[] args) {
        // testButton1();
        testButton2();
    }

    public static void testButton1() {
        JFrame frame = new JFrame();
        frame.setTitle("我的窗口");

        // 获取顶层容器的默认内容窗格
        Container contentPane = frame.getContentPane();
        JButton button = new JButton("确定");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String actionCommand = event.getActionCommand();
                Object source = event.getSource();
                if (source instanceof JButton) {
                    JButton button = (JButton) source;
                    String text = button.getText();
                    System.out.println("按钮被点击了：" + text);
                }
            }
        });
        contentPane.add(button, BorderLayout.SOUTH);
        // 设置窗口的图标
        // frame.setIconImage(new ImageIcon("./src/img/logo1.jpg").getImage());

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testButton2() {
        JFrame frame = new JFrame();
        frame.setTitle("我的窗口");

        // 创建一个面板
        JPanel contentPane = new JPanel();
        // 将面板设置为边界布局
        contentPane.setLayout(new BorderLayout());
        JButton button = new JButton("确定");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                String actionCommand = event.getActionCommand();
                Object source = event.getSource();
                if (source instanceof JButton) {
                    JButton button = (JButton) source;
                    String text = button.getText();
                    System.out.println("按钮被点击了：" + text);
                }
            }
        });
        contentPane.add(button, BorderLayout.SOUTH);
        // 将面板设置为顶层容器的内容窗格
        frame.setContentPane(contentPane);
        // 设置窗口的图标
        // frame.setIconImage(new ImageIcon("./src/img/logo1.jpg").getImage());

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
